package com.hzspace.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Author: LiuHao
 * @Descirption:
 * @Date: 2020/3/1_23:13
 */
@Component
@Aspect
public class LogAspect {

    /**
     * 定义拦截规则
     * 第一个*指明返回值类型任意
     * com.hzspace.aop.service.*.*(..)
     * 指明com.hzspace.aop.service包下的任意类的任意方法
     * (..)指方法入参类型任意 个数任意
     */
    @Pointcut("execution(* com.hzspace.aop.service.*.*(..))")
    public void pointCut() {
    }

    @Before(value = "pointCut()")
    public void doBefore(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("doBefore==>" + name);
    }

    @After(value = "pointCut()")
    public void doAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("doAfter==>" + name);
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void doAfterReturning(JoinPoint joinPoint, Object result) {
        String name = joinPoint.getSignature().getName();
        System.out.println("doAfterReturning==>" + name + ", Return===>" + result);
    }

    @AfterThrowing(value = "pointCut()", throwing = "ex")
    public void doAfterThrowing(JoinPoint joinPoint, Exception ex) {
        String name = joinPoint.getSignature().getName();
        System.out.println("doAfterThrowing==>" + name + ", Exception Message===>" + ex.getMessage());
    }

    @Around(value = "pointCut()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        String name = proceedingJoinPoint.getSignature().getName();
        if(!"find".equals(name)){
            System.out.println("doAround Proceed Method Name" + name);
            return proceedingJoinPoint.proceed();
        }
        System.out.println("doAround doesn't Proceed Method Name" + name);
        return null;
    }

}

